package com.example.demo.model;

import com.example.demo.DistributionLock;
import com.example.demo.model.mapper.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resources;
import java.util.List;

@Service
public class StockService implements IStockService {


    @Autowired
    private StockMapper stockMapper;

    @Autowired
    @Qualifier("redis")
    private DistributionLock lock;

    @Override
    public void descCount() {
        stockMapper.descCount();
    }


    /**
     * 实现分布式锁
     * @param finalI
     */
    @Override
    public void descCountWithDistribution(int finalI) {
        Boolean flag = lock.acquire();
        if(flag){
            if(finalI % 3 == 0){
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            stockMapper.descCount();
            lock.release();
        }
    }

    @Override
    public List<Stock> getAll() {
        return stockMapper.selectAll();
    }
}
